English - French - German -Italian - Spanish -فارسي

Thursday January 27, 2005 12:27:49 ب.ظ

                                                                                                                   
 

آموزش پرولوگ به زبان اصلي

 

1. How to Run Prolog

The examples in this primer were all developed using either Quintus Prolog running on Digital Equipment Corporation MicroVAXes or using SWI Prolog on either Sun Sparks or in Windows on a PC. SWI (in Holland) has Prolog information and a download area. See ...
SWI-Prolog Home Page   (current as of 06/28/2004)
SWI-Prolog's home page has lots of information about SWI-Prolog, a download area, and documentation.

[Note added 06/28/2004.  In Chapter 6 we use XSB Prolog.  See Chapter 6 for references to XSB.]

The examples in this tutorial use a simplified form of interaction with a typical Prolog interpreter. The sample programs should execute similarly on any system using an Edinburgh-style Prolog interpreter or compiler.

To start the prolog interpreter type 'prolog'

$ prolog
after the VMS prompt '$', for Quintus Prolog, or type 'pl'
% pl
for SWI Prolog, after the Unix prompt '%'.

In Windows, SWI-Prolog installs a start icon that can be double clicked to initiate the interpreter. The interpreter then starts in its own window.

A startup message or banner may appear, and will soon be followed by a goal prompt similar to the following

?- _
Goals in Prolog are entered by the user following the '?- ' prompt.

Many Prologs have on-line help information. Quintus and SWI Prolog have extensive help information. This help is indexed and guides the user. To learn more about it, try

?- help(help).
Notice that all of the displayed symbols need to be typed in, followed by a carriage return.

To illustrate some particular interactions with prolog, consider the following sample session. Each file referred to is assumed to be a local file in the user's account, which was either created by the user, obtained by copying directly from some other public source, or obtained by saving a text file while using a web browser. The way to achieve the latter is either to follow a URL to the source file and then save, or to select text in a Prolog Tutorial web page, copy it, paste into a text editor window and then save to file. The comments /* ... */ next to goals are referred to in the notes following the session.

?- ['2_1.pl'].           /* 1. Load a program from a local file*/  
yes  
?- listing(factorial/2). /* 2. List program to the screen*/  
  
factorial(0,1).
  
factorial(A,B) :-  
           A > 0, 
           C is A-1,
           factorial(C,D),
           B is A*D. 
yes
  
?- factorial(10,What).     /* 3. Compute factorial of 10 */
What=3628800
 
?- ['2_7.pl'].          /* 4. Load another program */

?- listing(takeout).
  
takeout(A,[A|B],B).
takeout(A,[B|C],[B|D}) :-
          takeout(A,C,D).
yes

?- takeout(X,[1,2,3,4],Y).  /* 5. Take X out of list [1,2,3,4] */
X=1  Y=[2,3,4] ;              Prolog waits ... User types ';' and Enter
X=2  Y=[1,3,4] ;               again ...  
X=3  Y=[1,2,4] ;               again ...
X=4  Y=[1,2,3] ;               again ...
no                             Means: No more answers.

?- takeout(X,[1,2,3,4],_), X>3.  /* 6.  Conjunction of goals */
X=4 ;
no

?- halt.                         /* 7. Return to OS */
The italicized comments appearing at the right at various spots in a sample session were added with a text processor. They also serve as reference signposts for the notes after the session. We will discuss some of these points now, while others will be referred to in later discussions.

Notes:

1. A Prolog goal is terminated with a period "." In this case the goal was to load a program file. This "bracket" style notation dates back to the first Prolog implementations. Several files can be chain loaded by listing the filenames sequentially within the brackets, separated by commas. In this case, the file's name is 2_1.pl (programs corresponding to Section 7.1 of this tutorial), which contains two prolog programs for calculating the factorial of a positive integer. The actual program in the file is discussed in Section 2.1. The program file was located in the current directory. If it had not been, then the path to it would have to have been specified in the usual way. One can start Quintus Prolog and automatically load the program file fact.pro as follows:

$ prolog fact.pro
2. The built-in predicate 'listing' will list the program in memory -- in this case, the factorial program. The appearance of this listing is a little different than the appearance of the source code in the file, which we will see in Section 2.1. Actually, Quintus Prolog compiles programs unless predicates are declared to be dynamic. Compiled predicates do not have an interactive source listing that can be supplied by a 'listing' goal. So, in order to illustrate this Prolog interpreter feature, the predicates were declared as dynamic in the source code before this sample run.

3. The goal here, 'factorial(10,What)', essentially says "the factorial of 10 is What?". The word 'What' begins with an upper-case letter, denoting a logical variable. Prolog satisfies the goal by finding the value of the variable 'What'.

4. Both "programs" now reside in memory, from the two source files 2_1.pl and 2_7.pl. The 2_7.pl file has many list processing definitions in it. (See Section 2.7.)

5. In the program just loaded is a definition of the logical predicate 'takeout'. The goal 'takeout(X,[1,2,3,4],Y)' asks that X be taken out of list [1,2,3,4] leaving remainder list Y, in all possible ways. There are four ways to do this, as shown in the response. The 'takeout' predicate is discussed in Section 2.7. Note, however, how Prolog is prodded to produce all of the possible answers: After producing each answer, Prolog waits with a cursor at the end of the answer. If the user types a semicolon ';' , Prolog will look for a next answer, and so on. If the user just hits Enter, then Prolog stops looking for answers.

6. A compound or conjunctive goal asks that two individual goals be satisfied. Note the arithmetic goal (built-in relation), 'X>3'. Prolog will attempt to satisfy these goals in the left-to-right order, just as they would be read. In this case, there is only one answer. Note the use of an anonymous variable '_' in the goal, for which no binding is reported ("don't-care variable").

7. The 'halt' goal always succeeds and returns the user to the operating system.

 

شمارنده

 
صفحه اول
فارسي
English
French
German
Italian
Spanish

آموزشي

PASCAL
JAVA
C
C++
ASSEMBLY
LOGO 4
FOXPRO
MAYA5
FREEHAND
A+
UNIX
LINUX
AUTOCAD
PROLOG
FORTH
COBOL
LISP
FORTRAN
APL
ADA

علمي دانشگاهي

صفحه مقالات
پيام نور جهرم
ساختمان گسسته
مدار منطقي
زبان تخصصي
معماري كامپيوتري
الگوريتم و فلوچارت
ذخيره و بازيابي
ريزپردازنده
شبكه
پايگاه داده
هوش مصنوعي
شبيه سازي
گرافيك

سرگرمي و دانلود

كليپ رايگان
موسيقي رايگان
بازي رايگان
آلبوم عكس
رضا كوچولو
مسابقات

خدمات سايت

گفتگو با مشاور
صفحه افراد
ليست دارو
 

ارتباط

با مشاور
با سازنده